package com.itboye.textileexpres.collection;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.textileexpes.beans.ColletionFenyeBeans;
import com.itboye.textileexpes.presenter.ColloctionPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.util.XImageLoader;
import com.itboye.textileexptes.config.GlobalConfig;
//ActivityOrderSeaech
public class ActivityCollectionSeaech extends BaseActivity implements
		OnClickListener, Observer {
	ImageView back;
	ListView list;
	MyListadapter adapter;
	ColloctionPresenter colloctionPresenter;
	int pagesize = 1;
	String uid;
	ColletionFenyeBeans beans = new ColletionFenyeBeans();
	List<ColletionFenyeBeans.InfoBeans> listInfoBeans = new ArrayList<ColletionFenyeBeans.InfoBeans>();
	TextView search;
	EditText key_word;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_collect_search;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		uid = (String) SPUtils.get(ActivityCollectionSeaech.this, null,
				SpPublicParams.UID, "");
		colloctionPresenter = new ColloctionPresenter(this);

		adapter = new MyListadapter(this, listInfoBeans);
		list.setAdapter(adapter);
		search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if ((key_word.getText() + "").length() > 0) {
					colloctionPresenter.FenYeQuery("39", "10", pagesize + "",
							key_word.getText() + "", "");
				} else {
					toast("请输入关键词");
					return;
				}

			}
		});
	}

	class MyListadapter extends BaseAdapter {
		Context context;
		List<ColletionFenyeBeans.InfoBeans> list;

		public MyListadapter(Context context) {
			// TODO Auto-generated constructor stub
			this.context = context;
		}

		public MyListadapter(Context context,
				List<ColletionFenyeBeans.InfoBeans> list) {
			// TODO Auto-generated constructor stub
			this.context = context;
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list == null ? 0 : list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return list.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			viewHolder holder = null;
			if (convertView == null) {
				holder = new viewHolder();
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_goods_collection, null);
				holder.publish_time = (TextView) convertView
						.findViewById(R.id.publish_time);
				holder.title = (TextView) convertView.findViewById(R.id.title);
				holder.context = (TextView) convertView
						.findViewById(R.id.context);
				holder.number = (TextView) convertView
						.findViewById(R.id.number);
				holder.delete_img = (ImageView) convertView
						.findViewById(R.id.delete_img);
				holder.collection_img = (ImageView) convertView
						.findViewById(R.id.collection_img);
				holder.goods_img = (ImageView) convertView
						.findViewById(R.id.goods_img);
				convertView.setTag(holder);
			} else {
				holder = (viewHolder) convertView.getTag();
			}
			holder.delete_img.setVisibility(View.GONE);
			holder.collection_img.setVisibility(View.GONE);
			holder.title
					.setText(list.get(arg0).getFavorite_content().getName());
			holder.context.setText(list.get(arg0).getFavorite_content()
					.getSynopsis());
			holder.number.setText(list.get(arg0).getFavorite_content()
					.getProduct_code());
			XImageLoader.load(GlobalConfig.IMAGE_DOUBLE
					+ list.get(arg0).getFavorite_content().getId(),
					holder.goods_img);
			return convertView;
		}
	}

	class viewHolder {
		TextView title, context, publish_time, number;
		ImageView goods_img, collection_img, delete_img;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == ColloctionPresenter.ColloctFenye_success) {
				beans = (ColletionFenyeBeans) entity.getData();
				// Log.e("hu...collect", beans.getCount() + "");
				listInfoBeans.addAll(beans.getList());
				adapter.notifyDataSetChanged();
			} else if (entity.getEventType() == ColloctionPresenter.ColloctFenye_fail) {
				toast("" + entity.getData());
			}
		}
	}
}